package com.tchy.cloud.controller;

import com.tchy.cloud.entity.ABankEntity;
import com.tchy.cloud.entity.TSCityEntity;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 城市信息维护
 */
@Controller
@RequestMapping("/cityController")
public class CityController {
    private static final Logger logger = Logger.getLogger(TSCityEntity.class.getName());

    @Autowired
    private CommonService commonService;

    /**
     * 城市列表页面跳转
     *
     * @return
     */
    @RequestMapping(params = "index")
    public ModelAndView index() {
        return new ModelAndView("clound/city/index");
    }

    /**
     * 城市列表数据
     *
     * @param cityEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(TSCityEntity cityEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(TSCityEntity.class, dataGrid);
        cq.addOrder("id", SortDirection.desc);
        cq.add();
        // 查询条件组装
        HqlGenerateUtil.installHql(cq, cityEntity, request.getParameterMap());
        commonService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 新增或编辑页面跳转
     *
     * @param cityEntity
     * @param req
     * @return
     */
    @RequestMapping(params = "addorupdate")
    public ModelAndView addorupdate(TSCityEntity cityEntity, HttpServletRequest req) {
        if (cityEntity.getId() != null) {
            cityEntity = commonService.getEntity(TSCityEntity.class, cityEntity.getId());
        }
        req.setAttribute("cityEntity", cityEntity);
        return new ModelAndView("clound/city/addorupdate");
    }


    /**
     * 新增及修改的数据处理
     *
     * @param cityEntity
     * @return
     */
    @RequestMapping(params = "save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson save(TSCityEntity cityEntity) {
        AjaxJson j = new AjaxJson();
        String name = cityEntity.getName();
        try {
            if (cityEntity.getId() != null) {//修改
                TSCityEntity entity = commonService.get(TSCityEntity.class, cityEntity.getId());
                if (name.equals(entity.getName())) {//名称未改变
                    MyBeanUtils.copyBeanNotNull2Bean(cityEntity, entity);
                    commonService.saveOrUpdate(entity);
                } else {//名称改变过
                    List<Map<String, Object>> list = commonService.findForJdbc("select * from t_s_city where name =?", new Object[]{name});
                    if (!list.isEmpty())
                        j.setMsg("城市已经存在，不允许重复添加。");
                }
            } else {//保存
                List<Map<String, Object>> list = commonService.findForJdbc("select * from t_s_city where name =?", new Object[]{name});
                if (!list.isEmpty())
                    j.setMsg("城市已经存在，不允许重复添加。");
                else {
                    commonService.save(cityEntity);
                    j.setMsg("操作成功");
                }
            }

        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }

        return j;
    }


    /**
     * 删除
     *
     * @param cityEntity
     * @return
     */
    @RequestMapping(params = "del", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson del(TSCityEntity cityEntity) {
        AjaxJson j = new AjaxJson();
        try {
            if (cityEntity.getId() != null) {//删除
                TSCityEntity entity = commonService.get(TSCityEntity.class, cityEntity.getId());
                commonService.delete(entity);
                j.setMsg("操作成功");
            } else {//主键不存在
                j.setMsg("城市不存在，请重试。");
            }

        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }

        return j;
    }
}







